package edu.rit.hvz.android;

import edu.rit.hvz.android.DataLayer.MissionDBAdapter;
import edu.rit.hvz.android.DataLayer.NewsDBAdapter;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.TextView;

public class Mission extends Activity implements OnLongClickListener{
	private static final int REQUEST_CODE = 1;
	private static final int Edit_Mission = 2;
	
	private TextView title;
	private TextView content;
	private Cursor mission;
	private MissionDBAdapter db;
	private Long id;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.onActivityCreateSetTheme(this);
        setContentView(R.layout.mission_layout);
        db = new MissionDBAdapter(this);
        db.open();
        
        title = (TextView)findViewById(R.id.textView1);
        content = (TextView)findViewById(R.id.textView2);
        
        title.setOnLongClickListener(this);
		content.setOnLongClickListener(this);
		
		id = this.getIntent().getLongExtra(MissionDBAdapter.ROW_ID, -1);
		if( id != -1 ){
			mission = db.getMission(id);
			title.setText(mission.getString(mission.getColumnIndex(MissionDBAdapter.TITLE)));
			content.setText(mission.getString(mission.getColumnIndex(MissionDBAdapter.CONTENT)));
		}
		db.close();
    }
	
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, Edit_Mission, 0, "Edit Mission");
        MenuInflater mInflater = getMenuInflater();
		mInflater.inflate(R.layout.menu, menu);
        return super.onCreateOptionsMenu(menu);
        
    }
	
	 @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	    	case Edit_Mission:
	    		editMission();
	    		return true;
	    	case R.id.settings:
				this.startActivity(new Intent(this, Preferences.class));
				return true;
	        }
	        return false;
	    }
	
	public void editMission() {
		Intent intent = new Intent().setClass(this, EditMission.class);
		intent.putExtra(MissionDBAdapter.ROW_ID, id);
		
		this.startActivityForResult(intent, REQUEST_CODE);
	}

	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		editMission();
		return true;
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data) {
	    startActivity(getIntent());
	    finish();
	}
}
